﻿using Magic.Core;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System.Linq;
using System.Threading.Tasks;
using Magic.Application.Entity;
namespace Magic.Application
{
    /// <summary>
    /// Vender服务
    /// </summary>
    [ApiDescriptionSettings("Application",Name = "Vender", Order = 1)]
    public class VenderService : IVenderService, IDynamicApiController, ITransient
    {
        private readonly SqlSugarRepository<Vender> _rep;

        public VenderService(SqlSugarRepository<Vender> rep)
        {
            _rep = rep;
        }

        /// <summary>
        /// 分页查询Vender
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("/Vender/page")]
        public async Task<dynamic> Page([FromQuery] VenderInput input)
        {
            var entities = await _rep.Context.Queryable<Vender>()
                                     .WhereIF(!string.IsNullOrWhiteSpace(input.Name), u => u.Name.Contains(input.Name.Trim()))
                                     .WhereIF(!string.IsNullOrWhiteSpace(input.Contact), u => u.Contact.Contains(input.Contact.Trim()))
                                     .WhereIF(!string.IsNullOrWhiteSpace(input.Tel), u => u.Tel.Contains(input.Tel.Trim()))
                                     .WhereIF(!string.IsNullOrWhiteSpace(input.Address), u => u.Address.Contains(input.Address.Trim()))
                                     .WhereIF(!string.IsNullOrWhiteSpace(input.Country), u => u.Country.Contains(input.Country.Trim()))
                                     .WhereIF(!string.IsNullOrWhiteSpace(input.Location), u => u.Location.Contains(input.Location.Trim()))
                                     .WhereIF(!string.IsNullOrWhiteSpace(input.Email), u => u.Email.Contains(input.Email.Trim()))
                                     .ToPagedListAsync(input.PageNo, input.PageSize);
            return entities.XnPagedResult();
        }

        /// <summary>
        /// 增加Vender
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("/Vender/add")]
        public async Task Add(AddVenderInput input)
        {
            var entity = input.Adapt<Vender>();
            await _rep.InsertAsync(entity);
        }

        /// <summary>
        /// 删除Vender
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("/Vender/delete")]
        public async Task Delete(DeleteVenderInput input)
        {
            var entity = await _rep.FirstOrDefaultAsync(u => u.Id == input.Id);
            await _rep.DeleteAsync(entity);
        }

        /// <summary>
        /// 更新Vender
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("/Vender/edit")]
        public async Task Update(UpdateVenderInput input)
        {
            var entity = input.Adapt<Vender>();
            await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns:true).ExecuteCommandAsync();
        }

        /// <summary>
        /// 获取Vender
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("/Vender/detail")]
        public async Task<Vender> Get([FromQuery] QueryeVenderInput input)
        {
            return await _rep.FirstOrDefaultAsync(u => u.Id == input.Id);
        }

        /// <summary>
        /// 获取Vender列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("/Vender/list")]
        public async Task<dynamic> List([FromQuery] VenderInput input)
        {
            return await _rep.ToListAsync();
        }
    }
}
